import { getFactorySelectPage } from '@/api/merchant'
import { ref } from 'vue'

/**
 * 商家下拉选项列表服务
 */
export default function () {
  const getMerchantOptionListLoading = ref(false)
  const getMerchantOptionListParam = ref({
    limit: 50,
    name: '',
    page: 1,
    factoryId: '',
  })
  const merchantOptionList = ref<Array<{ id: string; name: string; select: boolean }>>([])

  const resetGetMerchantOptionListParam = () => {
    getMerchantOptionListParam.value = {
      limit: 50,
      name: '',
      page: 1,
      factoryId: '',
    }
  }

  const getMerchantOptionList = async () => {
    try {
      getMerchantOptionListLoading.value = true
      const res = await getFactorySelectPage(getMerchantOptionListParam.value)
      getMerchantOptionListLoading.value = false
      if (res.data.code === 20000) {
        merchantOptionList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (error) {
      getMerchantOptionListLoading.value = false
      return '获取商家选项列表失败，请稍后重试'
    }
  }

  return {
    merchantOptionList,
    getMerchantOptionListParam,
    getMerchantOptionListLoading,
    getMerchantOptionList,
    resetGetMerchantOptionListParam,
  }
}
